86b496e2d178b6dc23d6043ab082ca706c193023
3 * This file is intended to test magic variables in the parser
4 * It was inspired by Raymond & Matěj Grabovský commenting about r66200
6 * As of february 2011, it only tests some revisions and date related
9 * @author Antoine Musso
10 * @copyright Copyright © 2011, Antoine Musso
16 * @covers Parser::getVariableValue
18 class MagicVariableTest
extends MediaWikiTestCase
{
22 private $testParser = null;
25 * An array of magicword returned as type integer by the parser
26 * They are usually returned as a string for i18n since we support
27 * persan numbers for example, but some magic explicitly return
29 * @see MagicVariableTest::assertMagic()
31 private $expectedAsInteger = [
36 /** setup a basic parser object */
37 protected function setUp() {
40 $contLang = Language
::factory( 'en' );
41 $this->setMwGlobals( [
42 'wgLanguageCode' => 'en',
43 'wgContLang' => $contLang,
46 $this->testParser
= new Parser();
47 $this->testParser
->Options( ParserOptions
::newFromUserAndLang( new User
, $contLang ) );
49 # initialize parser output
50 $this->testParser
->clearState();
52 # Needs a title to do magic word stuff
53 $title = Title
::newFromText( 'Tests' );
54 # Else it needs a db connection just to check if it's a redirect
55 # (when deciding the page language).
56 $title->mRedirect
= false;
58 $this->testParser
->setTitle( $title );
62 * @param int $num Upper limit for numbers
63 * @return array Array of numbers from 1 up to $num
65 private static function createProviderUpTo( $num ) {
67 for ( $i = 1; $i <= $num; $i++
) {
75 * @return array Array of months numbers (as an integer)
77 public static function provideMonths() {
78 return self
::createProviderUpTo( 12 );
82 * @return array Array of days numbers (as an integer)
84 public static function provideDays() {
85 return self
::createProviderUpTo( 31 );
88 # ############## TESTS #############################################
90 # - those got copy pasted, we can probably make them cleaner
91 # - tests are lacking useful messages
95 /** @dataProvider provideDays */
96 public function testCurrentdayIsUnPadded( $day ) {
97 $this->assertUnPadded( 'currentday', $day );
100 /** @dataProvider provideDays */
101 public function testCurrentdaytwoIsZeroPadded( $day ) {
102 $this->assertZeroPadded( 'currentday2', $day );
105 /** @dataProvider provideDays */
106 public function testLocaldayIsUnPadded( $day ) {
107 $this->assertUnPadded( 'localday', $day );
110 /** @dataProvider provideDays */
111 public function testLocaldaytwoIsZeroPadded( $day ) {
112 $this->assertZeroPadded( 'localday2', $day );
117 /** @dataProvider provideMonths */
118 public function testCurrentmonthIsZeroPadded( $month ) {
119 $this->assertZeroPadded( 'currentmonth', $month );
122 /** @dataProvider provideMonths */
123 public function testCurrentmonthoneIsUnPadded( $month ) {
124 $this->assertUnPadded( 'currentmonth1', $month );
127 /** @dataProvider provideMonths */
128 public function testLocalmonthIsZeroPadded( $month ) {
129 $this->assertZeroPadded( 'localmonth', $month );
132 /** @dataProvider provideMonths */
133 public function testLocalmonthoneIsUnPadded( $month ) {
134 $this->assertUnPadded( 'localmonth1', $month );
139 /** @dataProvider provideDays */
140 public function testRevisiondayIsUnPadded( $day ) {
141 $this->assertUnPadded( 'revisionday', $day );
144 /** @dataProvider provideDays */
145 public function testRevisiondaytwoIsZeroPadded( $day ) {
146 $this->assertZeroPadded( 'revisionday2', $day );
151 /** @dataProvider provideMonths */
152 public function testRevisionmonthIsZeroPadded( $month ) {
153 $this->assertZeroPadded( 'revisionmonth', $month );
156 /** @dataProvider provideMonths */
157 public function testRevisionmonthoneIsUnPadded( $month ) {
158 $this->assertUnPadded( 'revisionmonth1', $month );
161 # ############## HELPERS ############################################
163 /** assertion helper expecting a magic output which is zero padded */
164 public function assertZeroPadded( $magic, $value ) {
165 $this->assertMagicPadding( $magic, $value, '%02d' );
168 /** assertion helper expecting a magic output which is unpadded */
169 public function assertUnPadded( $magic, $value ) {
170 $this->assertMagicPadding( $magic, $value, '%d' );
174 * Main assertion helper for magic variables padding
175 * @param string $magic Magic variable name
176 * @param mixed $value Month or day
177 * @param string $format Sprintf format for $value
179 private function assertMagicPadding( $magic, $value, $format ) {
180 # Initialize parser timestamp as year 2010 at 12h34 56s.
181 # month and day are given by the caller ($value). Month < 12!
183 $month = $value %
12;
189 sprintf( '2010%02d%02d123456', $month, $value )
192 # please keep the following commented line of code. It helps debugging.
193 // print "\nDEBUG (value $value):" . sprintf( '2010%02d%02d123456', $value, $value ) . "\n";
195 # format expectation and test it
196 $expected = sprintf( $format, $value );
197 $this->assertMagic( $expected, $magic );
201 * helper to set the parser timestamp and revision timestamp
204 private function setParserTS( $ts ) {
205 $this->testParser
->Options()->setTimestamp( $ts );
206 $this->testParser
->mRevisionTimestamp
= $ts;
210 * Assertion helper to test a magic variable output
211 * @param string|int $expected
212 * @param string $magic
214 private function assertMagic( $expected, $magic ) {
215 if ( in_array( $magic, $this->expectedAsInteger
) ) {
216 $expected = (int)$expected;
219 # Generate a message for the assertion
220 $msg = sprintf( "Magic %s should be <%s:%s>",
228 $this->testParser
->getVariableValue( $magic ),